package com.sun.androidexerciseproject.widget.android.test;

import android.Manifest;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.GestureDetector;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.sun.androidexerciseproject.widget.R;
import com.sun.androidexerciseproject.widget.fragment.DatePickerFragment;
import com.sun.androidexerciseproject.widget.fragment.TimePickerFragment;
import com.sun.androidexerciseproject.widget.permission.XPermission;
import com.sun.androidexerciseproject.widget.util.LocationUtils;
import com.sun.androidexerciseproject.widget.util.LogUtil;

public class Lesson2Activity extends AppCompatActivity {
    private Button mToast;
    private Button mCount;
    private TextView mText;
    private int count = 0;
    private GestureDetectorCompat mDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lessson2);
        mToast = findViewById(R.id.btn_toast);
        mCount = findViewById(R.id.btn_count);
        mText = findViewById(R.id.tv_test);
        mText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment dialogFragment = new TimePickerFragment();
                dialogFragment.show(getSupportFragmentManager(), "time");
            }
        });
        mToast.setOnClickListener(v -> {
            Toast.makeText(Lesson2Activity.this, getString(R.string.android_lesson1_title), Toast.LENGTH_SHORT).show();
            DialogFragment dialogFragment = new DatePickerFragment();
            dialogFragment.show(getSupportFragmentManager(), "date");
        });
        mCount.setOnClickListener(v -> {
            count++;
            mText.setText(String.valueOf(count));
            PopupMenu popupMenu = new PopupMenu(Lesson2Activity.this, mToast);
            popupMenu.getMenuInflater().inflate(R.menu.bottom_nav_menu, popupMenu.getMenu());
            popupMenu.setOnMenuItemClickListener(item -> {
                int id = item.getItemId();
                if (id == R.id.navigation_home) {
                    Toast.makeText(Lesson2Activity.this, "popupMenu", Toast.LENGTH_LONG).show();
                }
                return true;
            });
            popupMenu.show();

        });
        mText.setText(String.valueOf(count));
        doPermission();
        mDetector = new GestureDetectorCompat(this, new MyGestureListener());
        registerForContextMenu(mCount);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_setting, menu);
    }

    private void getLocation() {
        String location = LocationUtils.getInstance().getLocations(this);
        LogUtil.log("getLocation location = " + location);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_fav:
                Toast.makeText(this, "收藏被点击", Toast.LENGTH_LONG).show();
                break;
        }
        return super.onContextItemSelected(item);
    }

    /**
     * 动态请求地理位置权限
     */
    private void doPermission() {
        XPermission.requestPermissions(this, 101, new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                new XPermission.OnPermissionListener() {
                    @Override
                    public void onPermissionGranted() {
                        getLocation();
                    }

                    @Override
                    public void onPermissionDenied() {
                        XPermission.showTipsDialog(Lesson2Activity.this);
                    }
                });
    }

    private static class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

        private static final String DEBUG_TAG = "Gestures";

        @Override
        public boolean onDown(MotionEvent event) {
            Log.d(DEBUG_TAG, "onDown: " + event.toString());
            return true;
        }

        @Override
        public boolean onFling(MotionEvent event1, MotionEvent event2,
                               float velocityX, float velocityY) {
            Log.d(DEBUG_TAG, "onFling: " +
                    event1.toString() + event2.toString());
            return true;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.mDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }
}
